package vn.com.shoppie.network;

import org.xml.sax.Attributes;

import vn.com.shoppie.sobject.Campaign;
import vn.com.shoppie.sobject.Gift;
import vn.com.shoppie.sobject.Merchant;
import vn.com.shoppie.sobject.Product;
import vn.com.shoppie.sobject.ShoppieObject;
import vn.com.shoppie.sobject.Slider;
import vn.com.shoppie.sobject.Store;
import vn.com.shoppie.sobject.User;

public class WiModelProtocol implements OnParseListener {
	private ShoppieObject _currentObject = null;

	WiModelManager mng;

	public WiModelProtocol(WiModelManager mng) {
		this.mng = mng;
		init();
	}

	private void init() {
	}

	public void parseAttributes(String elementName, Attributes atts) {

	}

	public void endElement(String element) {
		if (element.equals(Campaign.CLASS_UNIQUE) || element.equals(User.CLASS_UNIQUE) || element.equals(Store.CLASS_UNIQUE) || element.equals(Product.CLASS_UNIQUE) || element.equals(Gift.CLASS_UNIQUE) || element.equals(Merchant.CLASS_UNIQUE) || element.equals(Slider.CLASS_UNIQUE)) {
			mng.addShoppieObject(_currentObject);
			// Log.e("object: "+_crtObject, "element: "+element);
		}
		_crtElement = "";
	}

	String _crtObject;
	String _crtElement;

	@Override
	public void charactor(String content) {
		if (content == null || _crtObject == null || _crtElement == null) {
			System.out.println("NULL: " + _crtObject + "-" + _crtElement);
			return;
		}
		try {
			// CAMPAIGN
			if (_crtObject.equals(Campaign.CLASS_UNIQUE)) {
				if (_crtElement.equals("campaignId")) {
					((Campaign) _currentObject).id = content;
				} else if (_crtElement.equals("merchId")) {
					((Campaign) _currentObject).merchId = content;
				} else if (_crtElement.equals("campaignName")) {
					((Campaign) _currentObject).name = content;
				} else if (_crtElement.equals("campaignDesc")) {
					((Campaign) _currentObject).desc = content;
				} else if (_crtElement.equals("campaignImage")) {
					((Campaign) _currentObject).linkImage = content;
				}
			}

			// USER REGISTER
			else if (_crtObject.equals(User.CLASS_UNIQUE)) {
				if (_crtElement.equals("dataValue")) {
					((User) _currentObject).custId = content;
				}
			}

			// STORE
			else if (_crtObject.equals(Store.CLASS_UNIQUE)) {
				if (_crtElement.equals("merchId")) {
					((Store) _currentObject).merchId = content;
				} else if (_crtElement.equals("storeId")) {
					((Store) _currentObject).storeId = content;
				} else if (_crtElement.equals("storeCode")) {
					((Store) _currentObject).storeCode = content;
				} else if (_crtElement.equals("storeName")) {
					((Store) _currentObject).storeName = content;
				} else if (_crtElement.equals("storeAddress")) {
					((Store) _currentObject).storeAddress = content;
				} else if (_crtElement.equals("latitude")) {
					((Store) _currentObject).latitude = content;
				} else if (_crtElement.equals("longtitude")) {
					((Store) _currentObject).longtitude = content;
				} else if (_crtElement.equals("thumbnail")) {
					((Store) _currentObject).thumbnail = content;
				} else if (_crtElement.equals("merchCatId")) {
					((Store) _currentObject).merchCatId = content;
				}
			}

			// PRODUCT
			else if (_crtObject.equals(Product.CLASS_UNIQUE)) {
				if (_crtElement.equals("productId")) {
					((Product) _currentObject).productId = content;
				} else if (_crtElement.equals("merchId")) {
					((Product) _currentObject).merchId = content;
				} else if (_crtElement.equals("productName")) {
					((Product) _currentObject).productName = content;
				} else if (_crtElement.equals("longDesc")) {
					((Product) _currentObject).longDesc = content;
				} else if (_crtElement.equals("likedNumber")) {
					((Product) _currentObject).likedNumber = content;
				} else if (_crtElement.equals("price")) {
					((Product) _currentObject).price = content;
				} else if (_crtElement.equals("oldPrice")) {
					((Product) _currentObject).oldPrice = content;
				} else if (_crtElement.equals("productImage")) {
					((Product) _currentObject).productImage = content;
				}
			}

			// GIFT
			else if (_crtObject.equals(Gift.CLASS_UNIQUE)) {
				if (_crtElement.equals("giftId")) {
					((Gift) _currentObject).giftId = content;
				} else if (_crtElement.equals("merchId")) {
					((Gift) _currentObject).merchId = content;
				} else if (_crtElement.equals("giftName")) {
					((Gift) _currentObject).giftName = content;
				} else if (_crtElement.equals("description")) {
					((Gift) _currentObject).description = content;
				} else if (_crtElement.equals("pieQty")) {
					((Gift) _currentObject).pieQty = content;
				} else if (_crtElement.equals("giftImage")) {
					((Gift) _currentObject).giftImage = content;
				} else if (_crtElement.equals("redeemQty")) {
					((Gift) _currentObject).redeemQty = content;
				}
			}

			// SLIDER
			else if (_crtObject.equals(Slider.CLASS_UNIQUE)) {
				if (_crtElement.equals("slideId")) {
					((Slider) _currentObject).slideId = content;
				} else if (_crtElement.equals("slideDesc")) {
					((Slider) _currentObject).slideDesc = content;
				} else if (_crtElement.equals("slideLink")) {
					((Slider) _currentObject).slideLink = content;
				} else if (_crtElement.equals("slideImage")) {
					((Slider) _currentObject).slideImage = content;
				}
			}

			// MERCHANTS
			else if (_crtObject.equals(Merchant.CLASS_UNIQUE)) {
				if (_crtElement.equals("merchId")) {
					((Merchant) _currentObject).merchId = content;
				} else if (_crtElement.equals("merchName")) {
					((Merchant) _currentObject).merchName = content;
				} else if (_crtElement.equals("merchCatId")) {
					((Merchant) _currentObject).merchCatId = content;
				} else if (_crtElement.equals("merchImage")) {
					((Merchant) _currentObject).merchImage = content;
				} else if (_crtElement.equals("merchBanner")) {
					((Merchant) _currentObject).merchBanner = content;
				} else if (_crtElement.equals("merchDescription")) {
					((Merchant) _currentObject).merchDescription = content;
				}
			}
		} catch (NullPointerException e) {
			e.printStackTrace();
		}
	}

	public void startElement(String element) {
		_crtElement = element;
		// CAMPAIGN
		if (element.equals(Campaign.CLASS_UNIQUE)) {
			String[] values = new String[Campaign.NUM_FIELDS];
			_currentObject = new Campaign(-1, values);
			_crtObject = element;
			return;
		}
		// USER REGISTER
		if (element.equals(User.CLASS_UNIQUE)) {
			String[] values = new String[User.NUM_FIELDS];
			_currentObject = new User(-1, values);
			_crtObject = element;
			return;
		}

		// STORE
		if (element.equals(Store.CLASS_UNIQUE)) {
			String[] values = new String[Store.NUM_FIELDS];
			_currentObject = new Store(-1, values);
			_crtObject = element;
			return;
		}

		// PRODUCT
		if (element.equals(Product.CLASS_UNIQUE)) {
			String[] values = new String[Product.NUM_FIELDS];
			_currentObject = new Product(-1, values);
			_crtObject = element;
			return;
		}

		// GIFT
		if (element.equals(Gift.CLASS_UNIQUE)) {
			String[] values = new String[Gift.NUM_FIELDS];
			_currentObject = new Gift(-1, values);
			_crtObject = element;
			return;
		}
		
		// SLIDER
		if (element.equals(Slider.CLASS_UNIQUE)) {
			String[] values = new String[Slider.NUM_FIELDS];
			_currentObject = new Slider(-1, values);
			_crtObject = element;
			return;
		}

		// MERCHANT
		if (element.equals(Merchant.CLASS_UNIQUE)) {
			String[] values = new String[Merchant.NUM_FIELDS];
			_currentObject = new Merchant(-1, values);
			_crtObject = element;
			return;
		}
	}

	public void startDocument() {
	}

	public void finishDocument() {
	}

}
